//
//  CustomDatePicker.m
//  SinOpecHR
//
//  Created by Ray on 9/9/15.
//  Copyright (c) 2015 Siloon.KevinW. All rights reserved.
//

#import "CustomDatePicker.h"

@implementation CustomDatePicker

- (id)initWithFrame:(CGRect)frame date:(NSString *)date datePickerMode:(UIDatePickerMode)mode dateMode:(NSInteger)dateMode
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.date = date;
        self.pickerMode = mode;
        self.dateMode = dateMode;
        [self configureView];
        return self;
    }
    return nil;
}

- (void)configureView
{
    NSDate *date;
    if (self.dateMode == 0) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        date = [formatter dateFromString:self.date];
    } else if (self.dateMode == 1) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        date = [formatter dateFromString:self.date];
        
    }
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 180, self.frame.size.width, 180)];
    self.datePicker.datePickerMode = self.pickerMode;
    self.datePicker.date = date;
    [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.datePicker];
    
    self.cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 40, 20)];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(datePickerCancel:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cancelButton];
    
    self.confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - self.cancelButton.frame.size.width - self.cancelButton.frame.origin.x, self.cancelButton.frame.origin.y, self.cancelButton.frame.size.width, self.cancelButton.frame.size.height)];
    [self.confirmButton setTitle:@"确认" forState:UIControlStateNormal];
    [self.confirmButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.confirmButton addTarget:self action:@selector(datePickerConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.confirmButton];
}

- (void)dateChanged:(id)sender
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (self.dateMode == 0) {
        [formatter setDateFormat:@"yyyy-MM-dd"];
    } else if (self.dateMode == 1) {
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    }
    self.date = [formatter stringFromDate:self.datePicker.date];
}

- (void)datePickerCancel:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(cancelButtonClickedInCustomDatePicker:)]) {
        [self.delegate cancelButtonClickedInCustomDatePicker:self];
    }
}

- (void)datePickerConfirm:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(confirmButtonClickedInCustomDatePicker:)]) {
        [self.delegate confirmButtonClickedInCustomDatePicker:self];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
